#飞机游戏项目
import pygame
from pygame.locals import *
import time,random


class HeroPlane:
	'''玩家飞机（英雄）'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self.bullet_list =[]#存储发出去的子弹


	def display(self):
		'''绘制子弹'''
		for bullet in self.bullet_list:
			bullet.display()
			# bullet.move()
			#移动子弹
			if bullet.move():
				self.bullet_list.remove(bullet)

		'''绘制玩家飞机'''
		self.screen.blit(self.image,(self.x,self.y))

	def move_left(self):
		'''左移飞机'''
		self.x -=5
		if self.x <=0 :
			self.x=0

	def move_right(self):
		'''右移动飞机'''
		self.x +=5
		if self.x >406:
			self.x = 406

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		print(len(self.bullet_list))


class EnemyPlane:
	'''敌机类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(408))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images/e"+str(random.choice(range(3)))+".png")
		self.bullet_list = []

	def display(self):
		# #绘制敌机子弹
		for bt in self.bullet_list:
			bt.display()
			if bt.move():
				self.bullet_list.remove(bt)
		
		#绘制敌机
		self.screen.blit(self.image,(self.x,self.y))

	def move(self,hero):
		self.y += 5
		#遍历玩家子弹，并做碰撞检测
		if self.y>512:
			return True	

		for bo in hero.bullet_list:
			if bo.x>self.x+12 and bo.x < self.x + 92 and bo.y > self.y + 20 and bo.y<self.y+60:
				hero.bullet_list.remove(bo)	
				return True

		#
	def fire(self,hero):
			if random.choice(range(50)) ==10:
				self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))

			

class EnemyBullet:
	'''敌机子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x+40
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/epd.png")

	def display(self):
		'''绘制敌机子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		self.y +=10
		if self.y > 600:
			return True
		

# class Boom:
# 	'''爆炸效果类'''
# 	def __init__(self,screen_temp,x,y,inum):
# 		if inum == 0:
# 			self.x=x-12
# 			self.y=y-27
# 		else:
# 			self.x=x
# 			self.y=y

# 		self.screen=screen_temp
# 		self.image = pygame.image.load("./images/bomb" + str(inum) +".png")
# 		self.disflag = 10
# 		self.inum = inum

# 	def display(self):
# 		self.screen.blit(self.image,(self.x,self.y))
# 		self.disflag -= 1

class Boom:
    """爆炸效果类"""
    def  __init__(self,screen_temp,x,y,inum):
    	if inum == 0:
    		self.x = x - 12
    		self.y = y - 27
    	else:
    		self.x = x
    		self.y = y

    	self.screen = screen_temp
    	self.image = pygame.image.load("./images/bomb" + str(inum) +".png")#不同阶段对象采用不同图片素材
    	self.disflag = 10
    	self.inum = inum

    def display(self):
    	self.screen.blit(self.image,(self.x,self.y))
    	self.disflag -= 1





class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x+53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		self.y -=10
		if self.y < -20:
			return True


def key_control(hero_temp):
	'''键盘控制函数'''
	#获取时间，比如按键等
	for event in pygame.event.get():
		if event.type ==QUIT:
			print("exit")
			exit()

	#获取按下的键（返回的是元祖）
	pressed_keys =pygame.key.get_pressed()

	#判断是否按下a或者left键
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("left...")
		hero_temp.move_left()

	#检测是否按下d或者right键
	if pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("right...")
		hero_temp.move_right()

	#判断是否是空格键
	if pressed_keys[K_SPACE]:
		print("space...")
		hero_temp.fire()


#创建游戏主页窗口，并添加滚动背景
def main():
	'''游戏主程序执行函数'''
	#1、创建窗口
	screen = pygame.display.set_mode((512,568),0,0)
	
	#2、创建一个游戏背景图片
	background = pygame.image.load("./images/bg1.jpg")

	m=-968#初始化游戏背景图片标轴y的值

	#3.创建玩家飞机
	hero = HeroPlane(screen)
	
	#定义敌机列表
	enemylist = []
	#定义爆炸列表
	boomlist = []
	#定义玩家是否生存标志
	isalive = True

	while True:
		#绘制位图
		screen.blit(background,(0,m))
		m+=2
		if m>=-200:
			m=-968

		#显示玩家飞机
		hero.display()
		
		#调用键盘控制函数
		key_control(hero)

		#随机绘制敌机
		if random.choice(range(50))==10:
			enemylist.append(EnemyPlane(screen))
		#遍历敌机并绘制移动
		for em in enemylist:
			em.display()
			em.fire(hero)
			if em.move(hero):
				enemylist.remove(em)
			#遍历敌机子弹，并做碰撞检测
			for bo in em.bullet_list:
				if bo.x>hero.x+12 and bo.x<hero.x+92 and bo.y>hero.y+20 and bo.y<hero.y+60:
					isalive =False
					break
			#显示爆炸效果
		for boom in boomlist:
			if boom.disflag == 0:
				if boom.inum == 3:
					#爆炸最后一个阶段完成后，清除爆炸对象
					boomlist.remove(boom)
					print("3")
					break	
				else:#爆炸某一阶段完成，清除并插入	下一阶段的爆炸
					boomlist.remove(boom)
					print(boom.inum)
					boomlist.append(Boom(screen,boom.x,boom.y,boom.inum + 1))
			boom.display()
		#显示爆炸效果
        # for boom in boomlist:
       	# 	if boom.disflag == 0:
        # 		if boom.inum == 3:#爆炸效果最后阶段显示完后，爆炸对象从列表中删除
        # 			boomlist.remove(boom)
        # 			break
        # 		else:#爆炸对象某阶段显示完毕，清除并插入下一阶段爆炸对象
        # 			boomlist.remove(boom)
        # 			boomlist.append(Boom(screen,boom.x,boom.y,boom.inum + 1))
        # 	boom.display()

		#更新屏幕显示
		pygame.display.update()

		#如果玩家生存状态为false，则game over
		if isalive ==False:
			gameover = pygame.image.load("./images/gameover.png")
			screen.blit(gameover,(131,159))
			pygame.display.update()
			print("游戏结束！")
			time.sleep(3)
			break
		#定时睡眠
		time.sleep(0.04)
	
		

		



#判断是否是主程序，若是就执行主程序
if __name__ =="__main__":
	main()